var logger = Logger.getLogger();
var Board = Class(
	{	handleSelect: function(pos){
			logger.debug("handleSelect:("+pos.i+","+pos.j+")");
			if(pos){
				var square = this.getSquare(pos);
				if(square && square.pieceColor==this.turn){
					this.selected = pos;
				}
				logger.debug("piece at ("+pos.i+","+pos.j+")");
			}
			var
		},
		handleRelease : function(pos){
			logger.debug("handleMove:("+pos.i+","+pos.j+")");
			var src = this.selected;
			this.selected = false;
			if(!src){
				return false; //do nothing if no piece is previously picked
			}
			
			var dst = pos?pos:this.selected;
			logger.debug("apply move: ("+src.i+","+src.j+") -> ("+dst.i+","+dst.j+")");
		},
		handleMove : function(pos){
			logger.debug("handleRelease:("+pos.i+","+pos.j+")");
			var src = this.selected;
			this.selected = false;
			if(!src){
				return false; //do nothing if no piece is previously picked
			}
			
			var dst = pos?pos:this.selected;
			logger.debug("apply move: ("+src.i+","+src.j+") -> ("+dst.i+","+dst.j+")");
		},
		getSquare: function(pos){
			return this.squares[pos.i][pos.j];
		},
		init: function(_game){
			
			for(var i=0;i<8;i++){
				for(var j=0;j<8;j++){
					var piece = _game.at(i,j);
					if(piece){
						var img = document.createElement("img");
						img.src = "img\\"+piece.id+"-"+piece.color+".gif";
						this.getSquare({i:i,j:j}).appendChild(img);
					}
				}
			}
		}
	},
	function( _table ){
		if( (!_table) || _table.tagName != "TABLE" ){
			throw new Error("Board must use a table!");
		}
		this.dom = _table;
		this.squares = [];
		for(var i=0;i<8;i++){
			var line = document.createElement("tr");
			this.squares[i] = [];
			for(var j=0;j<8;j++){
				var td = document.createElement("td");
				td.className =(i+j)%2==0?"white":"black";
				td.isGameSquare = true;
				line.appendChild(td);	
				td.i=i;
				td.j=j;
				this.squares[i][j] = td;
			}
			this.dom.appendChild(line);
		}
		wrapEvent(this);
	}
);
var Square = Class(
		{
		},
		function (_color){
			this.dom = document.createElement("td");
			this.dom.className =(i+j)%2==0?"white":"black";
		}
);
function findFireSquare(_ev){
	_ev = _ev?_ev:window.event;
	var src = _ev.target;
	while(src&&!src.isGameSquare&&src.parentNode){
		src = src.parentNode;
	}
	return src&&src.isGameSquare ? {i:src.i,j:src.j} : {i:-1,j:-1};
}
function wrapEvent(_board){
	
	_board.dom.onmousedown = function(_ev){
		var pos = findFireSquare(_ev);
		Board.prototype.handleSelect.call(_board,pos);
		return false;
	}
	_board.dom.onmousemove = function(_ev){
		if(!_board.lastPos){
			_board.lastPos = {x:_ev.x,y: _ev.y};
			_board.lastCycle = 0;
			_board.clockCycles = 0;
			window.setInterval( function(){
				_board.clockCycles ++;
			},100) ;
		}
		var lastPos = _board.lastPos;
		var dis = null;
		if(lastPos){
			dis = [_ev.x - lastPos.x, _ev.y - lastPos.y];
			dis[0] = dis[0] > 0 ? dis[0] : -dis[0];
			dis[1] = dis[1] > 0 ? dis[1] : -dis[1];
		}
		if(((!dis)||dis[0]>2||dis[1]>2)&& _board.lastCycle != _board.clockCycles){
			logger.debug(dis);
			logger.debug(_board.clockCycles);
			var pos = findFireSquare(_ev);
			Board.prototype.handleMove.call(_board,pos);
			_board.lastCycle = _board.clockCycles;
			_board.lastPos = {x:_ev.x, y:_ev.y};
		}
		if(!lastPos) lastPos = {x:_ev.x,y: _ev.y};
		return false;
	}
	document.onmouseup = function(_ev){
		var pos = findFireSquare(_ev);
		Board.prototype.handleRelease.call(_board,pos);
		return false;
	}
}
